/*
 * Copyright (C) 2016 huanghaibin_dev <huanghaibin_dev@163.com>
 * WebSite https://github.com/MiracleTimes-Dev
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.talkweb.calendarview;


import ohos.agp.render.Canvas;
import ohos.app.Context;
import ohos.global.resource.Element;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * 默认年视图
 * Created by huanghaibin on 2018/10/9.
 */

public class DefaultYearView extends YearView {

    private int mTextPadding;

    public DefaultYearView(Context context) {
        super(context);
        mTextPadding = CalendarUtil.dipToPx(context, 3);
    }

    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawMonth(Canvas canvas, int year, int month, int x, int y, int width, int height) {

        ResourceManager resourceManager = getContext().getResourceManager();
        String[] array = null;
        try {
            array = resourceManager.getElement(ResourceTable.Strarray_month_string_array)
                    .getStringArray();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }

        if (array != null && month <= array.length) {
            String text = array[month - 1];
            canvas.drawText(mMonthTextPaint, text,
                    x + mItemWidth / 2 - mTextPadding,
                    y + mMonthTextBaseLine
            );
        }
    }

    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawWeek(Canvas canvas, int week, int x, int y, int width, int height) {

        ResourceManager resourceManager = getContext().getResourceManager();
        String[] array = null;
        try {
            array = resourceManager.getElement(ResourceTable.Strarray_year_view_week_string_array)
                    .getStringArray();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }

        if (array != null && week < array.length) {
            String text = array[week];
            canvas.drawText(mWeekTextPaint, text,
                    x + width / 2,
                    y + mWeekTextBaseLine
            );
        }
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        if (isSelected) {
            canvas.drawText(hasScheme ? mSchemeTextPaint : mSelectTextPaint, String.valueOf(calendar.getDay()),
                    cx,
                    baselineY
            );
        } else if (hasScheme) {
            canvas.drawText(calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint, String.valueOf(calendar.getDay()),
                    cx,
                    baselineY
            );

        } else {
            canvas.drawText(calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint,
                    String.valueOf(calendar.getDay()), cx, baselineY
            );
        }
    }
}
